Bibliotecas usadas:
library(tidyverse)
library(plotly)
library(ggrepel)
library(PerformanceAnalytics)
library(reshape2)
A coluna perfil é categórica e definimos a ordem das colunas no histograma passando um vetor para factor com a ordem desejada:
perfil = read.csv('data/perfil.csv')
levels = c("Conservador","Moderado","Agressivo")
perfil$perfil = factor(perfil$perfil, levels=levels)
ggplot(data = perfil) + geom_bar(aes(x=perfil))
Adicionado diversas opções:
ggplot(data = perfil) +
geom_bar(aes(x=perfil), fill="blue", color="red") +
geom_text(aes(x=perfil, label = ..count..), stat="count",vjust = -1) +
labs(title="Investidores",x="Categoria",y="Quantidade", caption="Banco Bom") +
theme_light() # para trocar o fundo
Rotacionado
ggplot(data = perfil) +
geom_bar(aes(x=perfil), fill="blue", color="red") +
geom_text(aes(x=perfil, label = ..count..), stat="count",hjust = -1) +
labs(title="Investidores",x="Categoria",y="Quantidade", caption="Banco Bom") +
coord_flip() +
theme_light() # para trocar o fundo
histograma básico:
sp = read.csv('data/sp.csv')
ggplot(data = sp) + geom_histogram(aes(x=idh))
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
Histograma com mais opções:
ggplot(data = sp) +
geom_histogram(aes(x=idh), bins=100) +
theme_minimal()
Scatterplot básico:
atlas = read.csv('data/atlas.csv')
ggplot(data = atlas) + geom_point(aes(x=renda, y=escolaridade))
Estradificação usando a idade:
ggplot(data = atlas) + geom_point(aes(x=renda, y=escolaridade,size=idade))
Estradificação usando a idade e mais uma dimensão de mortalidade:
ggplot(data = atlas) +
geom_point(aes(x=renda, y=escolaridade,size=idade,shape = mortalidade > 18)) +
theme_bw()
Adicionado linha de tendência:
ggplot(data = atlas) +
geom_point(aes(x=renda, y=escolaridade)) +
geom_smooth(aes(x=renda, y=escolaridade)) +
theme_bw()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
Marcando os pontos com a idade usando a função geom_text_repel:
ggplot(data = atlas) +
geom_point(aes(x=renda, y=escolaridade)) +
geom_text_repel(aes(x = renda, y = escolaridade),label = atlas$idade) +
theme_bw()
## Warning: ggrepel: 25 unlabeled data points (too many overlaps). Consider
## increasing max.overlaps
Se ao invés de geom_text_repel usarmos ggplotly, asism quando passamos o mouse em cima é mostrada a idade:
ggplotly(
ggplot(data = atlas) + geom_point(aes(x=renda, y=escolaridade, label = idade))
)
## Warning: Ignoring unknown aesthetics: label
Correlações das colunas 3 até a coluna 11 usando chart.Correlation :
chart.Correlation(atlas[, 3:11], histogram = T)
Correlações usando ggplot:
correlacoes <- cor(atlas[, 3:11])
correlacoes <- melt(correlacoes)
ggplot(correlacoes) +
geom_tile(aes(x = Var1, y = Var2, fill = value)) +
labs(x = NULL,y = NULL)
Trocando as cores:
ggplot(correlacoes) +
geom_tile(aes(x = Var1, y = Var2, fill = value)) +
geom_text(aes(x = Var1, y = Var2, label = round(value,2))) +
labs(x = NULL,y = NULL) +
scale_fill_gradient2(low = "darkblue",
mid = "white",
high = "darkred",
midpoint = 0)
### Boxplot
atlas_long <- melt(atlas[, c(2,5,6)], id.vars = "distritos")
ggplot(atlas_long) +
geom_boxplot(aes(x = variable, y = value, fill = variable))
Gráfico de linha básico:
covid = read.csv('data/covid.csv')
ggplot(data = covid) + geom_line(aes(x=t, y=cumulative_cases, color = country))